package com.cbs.java.component.network;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

/**
 * Created by broche on 10/16/15.
 */
public abstract class SimpleResponseHandler implements ResponseHandler {
    public void onTimeout(Request request, SocketTimeoutException exception) {
    }

    public void onUnknownHost(Request request, UnknownHostException exception) {
    }

    public void onConnectException(Request request, ConnectException exception) {
    }

    public void onOtherException(Request request, Exception exception) {
    }

    public abstract void onResponse(Response response);

    @Override
    public void onException(Request request, Exception e) {
        if (e.getClass().isAssignableFrom(SocketTimeoutException.class)) {
            onTimeout(request, ((SocketTimeoutException) e));
        } else if (e.getClass().isAssignableFrom(UnknownHostException.class)) {
            onUnknownHost(request, ((UnknownHostException) e));
        } else if (e.getClass().isAssignableFrom(ConnectException.class)) {
            onConnectException(request, ((ConnectException) e));
        } else {
            onOtherException(request, e);
        }
    }
}
